
#include "stdafx.h"
#include "interval.h"


map<UINT_PTR, IIntervalHandler*> Timer::s_handlerMap;

Timer::Timer()
{

}

void Timer::Start(IIntervalHandler* pIntervalHandler)
{
	m_nTimerId = SetTimer(NULL, 1, 1000 * 10, TimerProc);
	s_handlerMap[ m_nTimerId ] = pIntervalHandler;
}

UINT_PTR Timer::GetTimerId() const
{
	return m_nTimerId;
}

void Timer::Close()
{
	KillTimer(NULL, m_nTimerId);
	m_nTimerId = NULL;
}

void CALLBACK Timer::TimerProc( HWND, UINT, UINT_PTR nTimerId, DWORD )
{
	map<UINT_PTR, IIntervalHandler*>::iterator iter = s_handlerMap.find( nTimerId );
	if (s_handlerMap.end() != iter)
	{
		IIntervalHandler* pHandler = iter->second;
		if (NULL != pHandler)
		{
			pHandler->OnTimer();
		}
	}
}